package com.example.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ToDouble2 {
    public static String convertToDouble(String valueWithUnit) {
        String regex = "(\\d+(?:\\.\\d+)?)(\\D+)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(valueWithUnit.replaceAll(" ", "").replace("\n",""));

        if (matcher.find()) {
            String value = matcher.group(1);
            String unit = matcher.group(2);
            double number = Double.parseDouble(value);

            // 根据单位进行转换
            if (unit.equals("十")) {
                return ((int)(number * 1000))+"";
            } else if (unit.equals("百")) {
                return ((int)(number * 100))+"";
            } else if (unit.equals("千")) {
                return ((int)(number * 1000))+"";
            } else if (unit.equals("万")) {
                return ((int)(number * 10000))+"";
            } else {
                throw new IllegalArgumentException("Unsupported unit: " + unit);
            }
        } else {
            String value = valueWithUnit.replaceAll(" ", "").replace("\n","");
            return value;
        }
    }
}
